package com.ln.lnzw.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.blankj.utilcode.util.SPUtils;
import com.ln.lnzw.app.AppApplication;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;


/**
 * Created by wuwei on 2017/12/26.
 */
public class BaseActivity extends SupportActivity {
    public BaseActivity activity;
    public AppApplication application;
    private CompositeDisposable compositeDisposable;
    // 提示请登录的Dialog
    private AlertDialog toLoginDialog;

    private boolean mIsAddedView;
    private WindowManager.LayoutParams mNightViewParam;
    private WindowManager mWindowManager = null;
    private View mNightView = null;
    boolean isNight = false;
    public SPUtils spUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = this;
        application = AppApplication.getApp();
        spUtils = SPUtils.getInstance();
//        setMiuiStatusBarDarkMode(activity,true);//小米修改状态栏字体颜色
//        setMeizuStatusBarDarkIcon(activity,true);//魅族
    }

    public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
        Class<? extends Window> clazz = activity.getWindow().getClass();
        try {
            int darkModeFlag = 0;
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean setMeizuStatusBarDarkIcon(Activity activity, boolean dark) {
        boolean result = false;
        if (activity != null) {
            try {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                activity.getWindow().setAttributes(lp);
                result = true;
            } catch (Exception e) {
            }
        }
        return result;
    }

    //提示登录
//    public void showStartDialog(String s) {
//        toLoginDialog = null;
//        if (toLoginDialog == null) {
//            toLoginDialog = new AlertDialog.Builder(this).create();
//            toLoginDialog.show();
//            Window view = toLoginDialog.getWindow();
//            view.setContentView(R.layout.dialog_delete);
//            ((TextView) view.findViewById(R.id.tv_groupname)).setText(s);
//            view.findViewById(R.id.btn_delete).setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    toLoginDialog.dismiss();
//                    ActivityUtils.startActivity(activity, TRSPCenterActivity.class, R.anim.in_from_right, R.anim.out_to_left);
//                }
//            });
//            view.findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    toLoginDialog.dismiss();
//                    toLoginDialog = null;
//                }
//            });
//        }
//
//    }

    /**
     * @param disposable 添加事件队列
     */
    public void addDisposable(Disposable disposable) {
        if (compositeDisposable == null) {
            compositeDisposable = new CompositeDisposable();
        }
        compositeDisposable.add(disposable);
    }
//
    /**
     * 销毁事件队列
     */
    public void dispose() {
        if (compositeDisposable != null) compositeDisposable.dispose();
    }

    /**
     * 销毁
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        dispose();

//        changeToDay();
    }

//    /**
//     * 设置夜间模式
//     */
//    public void changeToNight() {
//        if (mIsAddedView == true)
//            return;
//        mNightViewParam = new WindowManager.LayoutParams(
//                WindowManager.LayoutParams.TYPE_APPLICATION,
//                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
//                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, //不让view获取焦点
//                PixelFormat.TRANSPARENT);
//        mWindowManager = getWindowManager();
//        mNightView = new View(this);
//        mNightView.setBackgroundResource(R.color.half_translate);
//        mWindowManager.addView(mNightView, mNightViewParam);
//        mIsAddedView = true;
//    }
//
//    /**
//     * 设置白天模式
//     */
//    public void changeToDay() {
//        if (mIsAddedView && mNightView != null) {
//            mWindowManager.removeViewImmediate(mNightView);
//            mWindowManager = null;
//            mNightView = null;
//            mIsAddedView = false;
//        }
//    }
//
//    @Override
//    protected void onResume() {
//        super.onResume();
//        if (spUtils.getBoolean(YEJIAN_SWITCH, false)) {
//            changeToNight();
//        } else {
//            changeToDay();
//        }
//    }
}
